package org.snail.other.service.impl;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.snail.other.bean.LevelDTO;
import org.snail.other.service.LevelService;
import org.snail.excel.annotation.ExcelSheetReaderProcessor;
import org.snail.other.bean.LevelDetailDO;
import org.snail.other.mapper.LevelDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.List;

@Service
public class LevelServiceImpl implements LevelService{

    private static final Logger logger =  Logger.getLogger(LevelServiceImpl.class);

    @Autowired
    private LevelDetailMapper levelDetailMapper;

    public String  levelFileUpload(CommonsMultipartFile file, HttpServletRequest request) {
        String outfilePath = "";
        String outfileName = "";
        FileInputStream input = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream out = null;
        try {
            logger.info("fileName：" + file.getOriginalFilename());
            String pathHome = request.getSession().getServletContext().getRealPath("upload");
            String path = pathHome + "/" +  new Date().getTime() + file.getOriginalFilename();
            File newFile = new File(path);
            if(!newFile.exists()) {
                newFile.mkdirs();
            }
            //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
            file.transferTo(newFile);
            //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
            input = new FileInputStream(newFile);  //读取的文件路径
            bufferedInputStream = new BufferedInputStream(input);
            XSSFWorkbook wb = new XSSFWorkbook(bufferedInputStream);
            Workbook workbook = new SXSSFWorkbook(wb);
            Sheet sheet =  wb.getSheet("level");
            ExcelSheetReaderProcessor processor = new ExcelSheetReaderProcessor();
            List<LevelDTO> list = processor.readFromExcelSheet(sheet, 1, LevelDTO.class);
            LevelDetailDO levelDetail = new LevelDetailDO();
            levelDetail.setEffective("1");
            levelDetail.setEndTime("11");
            levelDetail.setJavaAndWeb("javaAndWeb");
            levelDetail.setLevel("高级");
            levelDetail.setPrice("3000");
            levelDetail.setVersion("3");
            levelDetail.setWork("java");
//           levelDetailService.insertLevel(levelDetail);
            levelDetailMapper.insertLevel(levelDetail);
            for(LevelDTO levelDTO : list){
//                levelDao.insertLevel(levelDetailSheet);
            }
        } catch (Exception e) {
            logger.error(e.fillInStackTrace(), e);
        } finally {
            if(input!=null) {
                try {
                    input.close();
                } catch (IOException e) {
                    logger.error(e.fillInStackTrace(), e);
                }
            }
            if(bufferedInputStream!=null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    logger.error(e.fillInStackTrace(), e);
                }
            }
            if(out!=null) {
                try {
                    out.close();
                } catch (IOException e) {
                    logger.error(e.fillInStackTrace(), e);
                }
            }
        }
        return "upload/" + outfileName;
    }
}